﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.ComponentModel;
using System.Configuration.Install;
using System.Collections;

namespace Install
{
    public class Program
    {
        static void Main(string[] args)
        {
            string installFolder = Directory.GetCurrentDirectory();
            if (args != null && args.Length > 0)
            {
                installFolder = args[0].Replace("\"", "");
            }
            try
            {
                Run(installFolder);
            }
            catch (Exception ex)
            {
                EventLog.WriteEntry("Application", ex.ToString(), EventLogEntryType.Error);
            }
        }

        private static void Run(string installFolder)
        {
            EventLog.WriteEntry("Application", "Installing wscf.lightblue from folder [" + installFolder + "]", EventLogEntryType.Information);

            var folders = FindVisualStudioAddInFolders();
            ZetaFolderXCopy xcopy = new ZetaFolderXCopy();
            if (folders.Count == 0)
            {
                Console.Write("Visual Studio AddIn folder not found");
            }
            foreach (var folder in folders)
            {
                EventLog.WriteEntry("Application", "Installing wscf.lightblue addin: " + folder, EventLogEntryType.Information);
                xcopy.Copy(installFolder, folder,
                    new ZetaFolderXCopyOptions
                    {
                        CopyOnlyIfSourceIsNewer = true,
                        OverwriteExistingFiles = true
                    });
            }
        }

        static List<string> FindVisualStudioAddInFolders()
        {
            List<string> result = new List<string>();
            string s = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            string[] folders = Directory.GetDirectories(s);
            foreach (string folder in folders)
            {
                if (folder.ToLower().Contains("visual studio"))
                {
                    string addInFolder = Path.Combine(folder, "AddIns");
                    if (!Directory.Exists(addInFolder))
                    {
                        Directory.CreateDirectory(addInFolder);
                    }
                    result.Add(addInFolder);
                }
            }
            return result;
        }

    }
}
